﻿using System.ComponentModel;

namespace Sidvall.WorkItemManager.Workflow.WorkItems
{
    public class WorkItemContext : INotifyPropertyChanged
    {
        #region Public Members

        public event PropertyChangedEventHandler PropertyChanged;

        #region LoopCount

        private int _LoopCount;
        public int LoopCount
        {
            get
            {
                return _LoopCount;
            }
            set
            {
                if (_LoopCount == value)
                    return;
                _LoopCount = value;
                OnPropertyChanged("LoopCount");
            }
        }

        #endregion

        #region OnPropertyChanged

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, args);
        }

        #endregion
        #region Deserialize

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static WorkItemContext Deserialize(string value)
        {
            WorkItemContext item;

            item = null;
            try
            {
                if (!string.IsNullOrWhiteSpace(value))
                    item = Sidvall.Serialization.SerializerManager.Current.Deserialize<WorkItemContext>(value);
            }
            catch
            {
                item = null;
            }
            if (item == null)
            {
                item = new WorkItemContext();
                item.LoopCount = 5;
            }
            if (item.LoopCount < 1)
                item.LoopCount = 1;
            return item;
        }

        #endregion
        #region Serialize

        public static string Serialize(WorkItemContext item)
        {
            return Sidvall.Serialization.SerializerManager.Current.SerializeObject(item);
        }

        #endregion

        #endregion
    }
}
